/*
 * RecMarker.java
 *
 * Created on 30 April 2007, 10:55
 *
 */

package gui.markers;

import gui.hotspots.HotSpot;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


/**
 * Rectangle Marker Class
 * @author Slawek Wojcicki
 */
public class RectMarker extends FigureMarker implements Observer{
    
    private Point2D startPoint;
    private Rectangle2D rectangle;
    private static int instance_counter = 1;
    private HotSpot hsTL, hsTR, hsBL, hsBR;
    
    /** Creates a new instance of RecMarker */
    public RectMarker() {
        super( "Rectangle" + instance_counter++ );
        this.rectangle = null;
        this.startPoint = null;
        
    }
    
    public void drawFigure(Graphics2D g, boolean dragged) {
        g.setXORMode(Color.white);
        if(!dragged)
            g.setPaintMode();
        g.draw(rectangle);
        paintHotSpots(g);
    }
    
    private java.awt.geom.Rectangle2D.Double newRectangle2D(double endX,
            double endY)   {
        double ex;
        double ey;
        double width;
        double height;
        double x = startPoint.getX();
        double y = startPoint.getY();
        
        if (x < endX)         {
            ex = x;
            width = endX - x;
        } else        {
            ex = endX;
            width = x - endX;
        }
        
        if (y < endY)        {
            ey = y;
            height = endY - y;
        } else        {
            ey = endY;
            height = y - endY;
        }
        hsTL = new HotSpot(new Point2D.Double(ex,ey));
        hsTR = new HotSpot(new Point2D.Double(ex+width,ey));
        hsBL = new HotSpot(new Point2D.Double(ex,ey+height));
        hsBR = new HotSpot(new Point2D.Double(ex+width,ey+height));
        hsTL.addObserver(this);
        hsTR.addObserver(this);
        hsBL.addObserver(this);
        hsBR.addObserver(this);
        return new java.awt.geom.Rectangle2D.Double(ex, ey, width, height);
    }
    
    public void mouseDragged(int x, int y, Graphics2D g)    {
        g.setXORMode(Color.white);
        g.draw( rectangle ); // ???
        rectangle = newRectangle2D(x, y);
        g.draw( rectangle );
    }
    
 
    public void mousePressed(int x, int y, Graphics2D g)     {
        startPoint = new Point2D.Double(x, y);
        rectangle = newRectangle2D(x, y);
        g.draw( rectangle );
    }
    
    public boolean mouseReleased(int x, int y, Graphics2D g)    {
        g.setXORMode(Color.white);
        g.draw( rectangle );
        rectangle = newRectangle2D(x, y);
        g.setPaintMode();
        g.draw( rectangle );
        paintHotSpots(g);
        return ( (rectangle.getWidth() >0.0) || (rectangle.getHeight() >0.0) );
    }
    
    public boolean contains(Point2D p)    {
        return rectangle.intersects( p.getX() - 1.0, p.getY() - 1.0, 3.0, 3.0);
    }
    
    
    public void move(double x, double y)    {
        rectangle = new Rectangle2D.Double(
                rectangle.getX() + x, rectangle.getY() + y,
                rectangle.getWidth(), rectangle.getHeight() );
        
        hsTL.setPoint(new Point2D.Double(rectangle.getX(),rectangle.getY()));
        hsTR.setPoint(new Point2D.Double(rectangle.getX() + rectangle.getWidth(),rectangle.getY()));
        hsBL.setPoint(new Point2D.Double(rectangle.getX(),rectangle.getY() + rectangle.getHeight()));
        hsBR.setPoint(new Point2D.Double(rectangle.getX() + rectangle.getWidth(),rectangle.getY() + rectangle.getHeight()));
    }
    
    public List getHotSpots() {
        List l = new ArrayList();
        l.add(hsTL);
        l.add(hsTR);
        l.add(hsBL);
        l.add(hsBR);
        return l;
    }
    
    private void paintHotSpots(Graphics2D g) {
        hsTL.paint(g);
        hsTR.paint(g);
        hsBL.paint(g);
        hsBR.paint(g);
    }
    
    public void update(Observable obs, Object obj) {
        Point2D pTL = hsTL.getPoint();
        Point2D pTR = hsTR.getPoint();
        Point2D pBL = hsBL.getPoint();
        Point2D pBR = hsBR.getPoint();
        double x=0, y=0, w=0,h=0;
        if (obs == hsTL) {
            System.out.println("hsTL");
            x = pTL.getX();
            y = pTR.getY();
            w = pBR.getX()-pTL.getX();
            h = pBR.getY()-pTL.getY();
        } else if (obs == hsTR) {
            System.out.println("hsTR");
            x = rectangle.getX();
            y = pTR.getY();
            w = pTR.getX()-pTL.getX();
            h = pBR.getY()-pTR.getY();
        } else if (obs == hsBL) {
            System.out.println("hsBL");
            x = pBL.getX();
            y = rectangle.getY();
            w = pBR.getX()-pBL.getY();
            h = pBL.getY()-pTL.getY();
        } else if (obs == hsBR) {
            System.out.println("hsBR");
            x = rectangle.getX();
            y = rectangle.getY();
            //if (p.TL<)
            w = pBR.getX()-pTL.getX();
            h = pBR.getY()-pTL.getY();
        }
        
        hsTL.setPoint(new Point2D.Double(x,y));
        hsTR.setPoint(new Point2D.Double(x+w,y));
        hsTL.setPoint(new Point2D.Double(x,y+h));
        hsBR.setPoint(new Point2D.Double(x+w,y+h));
        
        x = (w < 0) ? (x + w) : x;
        y = (h < 0) ? (y + h) : y;
        w = (w < 0) ? -w : w;
        h = (h < 0) ? -h : h;
        
        rectangle = new Rectangle2D.Double(x,y,w,h);
        //rectangle = newRectangle2D(x,y);
        //ppaint(rectangle);
        
        
    }
    /*void ppaint(Rectangle2D r) {
        //Graphics2D g = (Graphics2D) getGraphics();
        g.draw(r);
    }*/
    
}
